.title_wrapper
  %h2 Distributors: Application Account

%h3 Create New Distributor Application Account

.form_view

  %h4 Enter Distributor Details
  - form_for(@application_accounts) do |f|
    = f.error_messages
    %p
      = f.label :name
      %br
      = f.text_field :name
    %p
      = f.label :distributor_id
      %br
      = f.collection_select :distributor_id, @distributors, :id, :name, {:prompt => 'Please Select Distributor'}
    %p
      = f.label :description
      %br
      = f.text_area :description
    %p
      = f.label :url
      %br
      = f.text_field :url
    %p      
    %br
    %h4 Application Attributes
    - # assigning bundle variations
    - @bundles_ids = (@application_accounts.bundle_variations.collect {|z| z.bundle_id}).uniq
    - @bundles_ids = @bundles_ids.length==0 ? [""] : @bundles_ids
    - @bundle_variations_id = (@application_accounts.bundle_variations.collect {|z| z.id})
    = hidden_field_tag "application_account[bundle_variation_ids][]"
    
    #div_add
      - @bundles_ids.each do |b|
        #origin_elem.bundles
          %br
          = f.label 'Select Bundle'
          %br
          = select_tag "bundles", "<option></option>"+options_from_collection_for_select(@bundles, :id, :name, b)
          - @bundles.each do |a|
            - check = (a.id.to_s == b.to_s)
            - visible = ( check ? "block;" : "none")
            ~ "<div id='variations' " + "style='display: #{visible};'>"
            - a.bundle_variations.each do |z| 
              = check_box_tag "application_account[bundle_variation_ids][]", z.id, ((@bundle_variations_id.include?(z.id) && check) ? true : false)
              - long_label = z.frequency.to_s+" ($#{z.price}"+ (z.trial_period ? ("; "+pluralize(z.trial_period.period, z.trial_period.frequency) + " trial period;" + (z.trial_period.need_credit_card ? " CC" : " No CC")) : "") + ")"
              = f.label long_label
            ~ "</div>"
    %p
    %br
    %a#addElem.hand
      Add Bundle

    %p

    %p
      = f.submit 'Create'
= link_to 'Back', application_accounts_path